<?php
class FeeAction extends AdminPublicAction{
	/** =============
	 列表
	 =================**/
	public function index()
	{
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "f.fee_id";
			$_GET['updown'] = $upDown = "desc";
				
		}
		$keyword = I('keyword','');
	
		if(tep_not_null($keyword))$where['f.fee_name']  = array('like','%'.$keyword.'%');
		
		import("ORG.Util.Page");
		$page = 10;
		$count = D('FeeView')->where($where)->count();
		$Page = new Page($count, $page);
		$list   = D('FeeView')->getAll($where,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		$show = $Page->show();
	
			
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
		$this->keyword = $keyword;
		$this->listing = $list;
		$this->page = $show;
		$this->left_fee = 'current';
		$this->display();
	
	}
	/** =============
	 添加 & 編輯
	 =================**/
	public function  detail(){
	
		$editId = I('id','',intval);	
		
	
		if(tep_not_null($editId)&&is_numeric($editId)){
			/*編輯*/
			$list   = D('FeeView')->getOne(array('f.fee_id' =>$editId));			
				
			
			$this->listInfo = $list;
			$this->editId = $editId;
			$this->addDate = $list['fee_added'];
			$this->action = 'editHandle';
			$this->actionName = 'Save';
		}else{
			//*添加*/
			$this->addDate = date('Y-m-d');
			$this->action = 'addHandle';
			$this->actionName = 'Add';
		}
		$this->left_fee = 'current';
		$this->display();
	
	}
	/** =============
	 添加处理
	 =================**/
	
	public function addHandle()
	{
		if(IS_POST){
			$data['fee_added'] = date("Y-m-d");			
			$data['fee_status'] = I('post.fee_status');
			$data['fee_name'] = I('post.fee_name');
			$data['fee_money'] = I('post.fee_money');
			$data['active_day'] = I('post.active_day')?I('post.active_day'):7;
			$insertId = M("fee")->add($data);
			
			S('success','Fee successfully added!');
			$this->redirect('index');
		}else{
			$this->redirect('index');
		}
			
	}
	/** =============
	 编辑处理
	 =================**/
	public function editHandle()
	{
		if(IS_POST){
				
			$id = I('post.editId');
			$data['fee_id'] = $id;			
			$data['fee_modification'] = date("Y-m-d H:i:s");			
			$data['fee_status'] = I('post.fee_status');
			$data['fee_name'] = I('post.fee_name');
			$data['fee_money'] = I('post.fee_money');
			$data['active_day'] = I('post.active_day')?I('post.active_day'):7;
			M("fee")->save($data);
			
			S('success','News update successfully!');
			$this->redirect('index');
		}else{
			$this->redirect('index');
		}
	}
	
	
	public function changeStatus(){
		$check = M("fee")->where(array("fee_id" => (int)$_GET['id']))->getField('fee_status');
		$news_status = ($check == 1)?0:1;
		M("fee")->where(array("fee_id" => (int)$_GET['id']))->setField('fee_status',$news_status);
		echo (int)$news_status;			
	}
	
	
}